package io.zhiller.storagedemo.storage.repo

import io.zhiller.storagedemo.storage.dao.NoteDAO
import io.zhiller.storagedemo.storage.db.RoomDB
import io.zhiller.storagedemo.storage.domain.Note
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.withContext

interface NoteRepo {
  suspend fun upsertNote(note: Note)
  suspend fun deleteNote(note: Note)
  fun getAllNotes(): Flow<List<Note>>
}

class NoteRepoImpl(
  private val noteDAO: NoteDAO,
  private val dispatcher: CoroutineDispatcher = Dispatchers.IO
) : NoteRepo {
  override suspend fun upsertNote(note: Note) = withContext(dispatcher) {
    noteDAO.upsertNote(note)
  }

  override suspend fun deleteNote(note: Note) = withContext(dispatcher) {
    noteDAO.deleteNote(note)
  }


  override fun getAllNotes(): Flow<List<Note>> {
    return noteDAO.getAllNotes()
  }
}